import 'dart:math';

import 'package:flutter/material.dart';

class GestureDemo extends StatefulWidget {
  const GestureDemo({super.key});

  @override
  State<GestureDemo> createState() => _GestureDemoState();
}

class _GestureDemoState extends State<GestureDemo> {
  String message = "";

  bool _switchSelected = true; //维护单选开关状态
  bool _checkboxSelected = true; //维护复选框状态

  double _sliderValue = 0;
  double _width = 0;

  @override
  void initState() {
    super.initState();

  }

  @override
  Widget build(BuildContext context) {
    _width = min(MediaQuery.of(context).size.width, 360);
    return SingleChildScrollView(
      child: Wrap(
        spacing: 4,
        runSpacing: 4,
        children: [
          _buildItem1(),
          _buildItemPan(),
          _buildItemScale(),
          _buildItemDrag(),
          _buildItemListener(),
          Center(child: Text(message)),
          _buildItemText(),
          _buildItemButtons(),
        ],
      ),
    );
  }

  _childItem(String text) {
    return Container(
      width: _width,
      height: _width,
      alignment: Alignment.center,
      color: Colors.orange,
      child: Text(
        text,
        style: const TextStyle(
          fontSize: 20,
          color: Colors.black45,
          decoration: TextDecoration.none,
          fontFamily: AutofillHints.name,
          fontWeight: FontWeight.normal,
        ),
      ),
    );
  }

  _buildItem1() {
    return GestureDetector(
      onTap: () => _setMessage("GestureDetector onTap"),
      onDoubleTap: () => _setMessage("onDoubleTap"),
      onLongPress: () => _setMessage("onLongPress"),
      onSecondaryTap: () => _setMessage("onSecondaryTap"),
      onSecondaryLongPress: () => _setMessage("onSecondaryLongPress"),
      onVerticalDragDown: (details) =>
          _setMessage("onVerticalDragDown: $details"),
      onVerticalDragStart: (details) =>
          _setMessage("onVerticalDragStart: $details"),
      onVerticalDragUpdate: (details) =>
          _setMessage("onVerticalDragUpdate: $details"),
      onVerticalDragEnd: (details) =>
          _setMessage("onVerticalDragEnd: $details"),
      onVerticalDragCancel: () => _setMessage("onVerticalDragCancel"),
      onHorizontalDragDown: (details) =>
          _setMessage("onHorizontalDragDown: $details"),
      onHorizontalDragStart: (details) =>
          _setMessage("onHorizontalDragStart: $details"),
      onHorizontalDragUpdate: (details) =>
          _setMessage("onHorizontalDragUpdate: $details"),
      onHorizontalDragEnd: (details) =>
          _setMessage("onHorizontalDragEnd: $details"),
      onHorizontalDragCancel: () => _setMessage("onHorizontalDragCancel"),
      child: _childItem("GestureDetector"),
    );
  }

  _buildItemPan() {
    return GestureDetector(
      onPanDown: (details) => _setMessage("onPanDown: $details"),
      onPanStart: (details) => _setMessage("onPanStart: $details"),
      onPanUpdate: (details) => _setMessage("onPanUpdate: $details"),
      onPanEnd: (details) => _setMessage("onPanEnd: $details"),
      onPanCancel: () => _setMessage("onPanCancel"),
      child: _childItem("GestureDetector onPan"),
    );
  }

  _buildItemScale() {
    return GestureDetector(
      onScaleStart: (details) => _setMessage("onScaleStart: $details"),
      onScaleUpdate: (details) => _setMessage("onScaleUpdate: $details"),
      onScaleEnd: (details) => _setMessage("onScaleEnd: $details"),
      child: _childItem("GestureDetector scale"),
    );
  }

  _buildItemDrag() {
    return Draggable(
      child: _childItem("Draggable"),
      feedback: _childItem("Draggable feedback"),
      onDragStarted: () => _setMessage("onDragStarted"),
      onDragUpdate: (details) => _setMessage("onDragUpdate: $details"),
      onDraggableCanceled: (velocity, offset) =>
          _setMessage("onDraggableCanceled: $velocity, $offset"),
      onDragEnd: (details) => _setMessage("onDragEnd: $details"),
      onDragCompleted: () => _setMessage("onDragCompleted"),
    );
  }

  _buildItemListener() {
    return Listener(
      onPointerSignal: (event) => _setMessage(
          "onPointerSignal kind=${event.kind}, buttons=${event.buttons}, position=${event.position}"),
      onPointerDown: (event) => _setMessage(
          "onPointerDown kind=${event.kind}, buttons=${event.buttons}, position=${event.position}"),
      onPointerUp: (event) => _setMessage(
          "onPointerUp kind=${event.kind}, buttons=${event.buttons}, position=${event.position}"),
      onPointerMove: (event) => _setMessage(
          "onPointerMove kind=${event.kind}, buttons=${event.buttons}, position=${event.position}"),
      onPointerHover: (event) => _setMessage(
          "onPointerHover kind=${event.kind}, buttons=${event.buttons}, position=${event.position}"),
      onPointerPanZoomStart: (event) => _setMessage(
          "onPointerPanZoomStart kind=${event.kind}, buttons=${event.buttons}, position=${event.position}"),
      onPointerPanZoomUpdate: (event) => _setMessage(
          "onPointerPanZoomUpdate kind=${event.kind}, buttons=${event.buttons}, position=${event.position}"),
      onPointerPanZoomEnd: (event) => _setMessage(
          "onPointerPanZoomEnd kind=${event.kind}, buttons=${event.buttons}, position=${event.position}"),
      onPointerCancel: (event) => _setMessage(
          "onPointerCancel kind=${event.kind}, buttons=${event.buttons}, position=${event.position}"),
      child: _childItem("Listener pointer"),
    );
  }

  _buildItemButtons() {
    return Column(
      children: [
        ElevatedButton(
          onPressed: () {
            _setMessage("ElevatedButton");
          },
          child: const Text("ElevatedButton"),
        ),
        OutlinedButton(
          onPressed: () {
            _setMessage("OutlinedButton");
          },
          child: const Text("OutlinedButton"),
        ),
        TextButton(
          onPressed: () {
            _setMessage("TextButton");
          },
          child: const Text("TextButton"),
        ),
        Switch(
          value: _switchSelected, //当前状态
          onChanged: (value) {
            setState(() {
              _switchSelected = value;
            });
          },
        ),
        Checkbox(
          value: _checkboxSelected,
          activeColor: Colors.red, //选中时的颜色
          onChanged: (value) {
            setState(() {
              _checkboxSelected = value ?? false;
            });
          },
        )
      ],
    );
  }

  _buildItemText() {
    return Column(
      children: [
        const Text("不可编辑的文字内容：abcdefg hijklmn opqrst uvwxyz"),
        RichText(
          text: const TextSpan(
            children: [
              TextSpan(
                  text: "TextSpan1",
                  style: TextStyle(color: Colors.greenAccent, fontSize: 24)),
              TextSpan(
                  text: "TextSpan2",
                  style: TextStyle(color: Colors.yellow, fontSize: 32)),
              TextSpan(
                  text: "TextSpan3",
                  style: TextStyle(color: Colors.red, fontSize: 44),
                  semanticsLabel: "这是Flutter")
            ],
          ),
        ),
        const TextField(
          decoration: InputDecoration(
            hintText: "单行输入框",
            border: OutlineInputBorder(),
          ),
        ),
        const TextField(
          maxLines: 8,
          decoration: InputDecoration(
            hintText: "多行输入框",
            border: OutlineInputBorder(),
          ),
        ),
        Image.asset('assets/images/6.jpg'),
        Slider(
            value: _sliderValue,
            onChanged: (value) {
              setState(() {
                _sliderValue = value;
              });
            }),
      ],
    );
  }

  _setMessage(String msg) {
    debugPrint("gestureDemo: $msg");
    setState(() {
      message = msg;
    });
  }
}
